import { cityjson } from '@/components/cityselector/districts.js'
export function getfromScopedSlots(fromInfo) {
  let ctrlsobject = { scopedSlots: [], isShowButReport: false }
  const planelist = fromInfo.layoutitemlist[0].widgetForm.list
  for (let pindex = 0; pindex < planelist.length; pindex++) {
    let controlList = []
    switch (planelist[pindex].type) {
      case 'gridPanel':
        controlList = planelist[pindex].columns[0].list
        findCotrl(controlList, ctrlsobject)
        break
      case 'tabs':
        controlList = planelist[pindex].options.panelist
        controlList.forEach(ee => {
          findCotrl(ee.columns[0].list, ctrlsobject)
        })
        break
      case 'collapse':
        controlList = planelist[pindex].options.panelist
        controlList.forEach(ee => {
          findCotrl(ee.columns[0].list, ctrlsobject)
        })
        break
      default:
        break
    }
  }
  return ctrlsobject
}

export function findPartLayout(partlayout, ctrlsobject) {
  findCotrl(partlayout.list, ctrlsobject)
}

export function findCotrl(controlList, ctrlsobject) {
  for (let i = 0; i < controlList.length; i++) {
    const controlItem = controlList[i]
    switch (controlItem.type) {
      case 'input':
        break
      case 'textarea':
        break
      case 'select':
        break
      case 'date':
        break
      case 'radio':
        break
      case 'checkbox':
        break
      case 'switch':
        ctrlsobject.isShowButReport = true
        ctrlsobject.scopedSlots.push({
          dataid: controlItem.options.filed,
          type: controlItem.type
        })
        break
      case 'slider':
        ctrlsobject.isShowButReport = true
        break
      case 'colorpicker':
        ctrlsobject.scopedSlots.push({
          dataid: controlItem.options.filed,
          type: controlItem.type
        })
        break
      case 'editor':
        break
      case 'rate':
        ctrlsobject.isShowButReport = true
        break
      case 'inputnumber':
        ctrlsobject.isShowButReport = true
        break
      case 'timepicker':
        break
      case 'upFilesComp':
        ctrlsobject.scopedSlots.push({
          dataid: controlItem.options.filed,
          type: controlItem.type,
          dragType: controlItem.options.uptype
        })
        break
      case 'datarelevance':
        ctrlsobject.scopedSlots.push({
          dataid: controlItem.options.filed + '_text',
          type: controlItem.type
        })
        break
      case 'user':
      case 'organ':
      case 'department':
      case 'role':
      case 'cityselector':
      case 'autonumber':
      case 'signname':
      case 'locationmap':
        ctrlsobject.scopedSlots.push({
          dataid: controlItem.options.filed,
          type: controlItem.type
        })
        break
      case 'partlayout':
        findPartLayout(controlList, ctrlsobject)
        break
      case 'listview':
        ctrlsobject.listview = []
        ctrlsobject.listview.push({
          controlId: controlItem.options.controlId,
          title: controlItem.title
        })
        ctrlsobject.listview.forEach(ee => {
          if (
            ee.type === 'radio' ||
            ee.type === 'checkbox' ||
            ee.type === 'select' ||
            ee.type === 'inputnumber' ||
            ee.type === 'rate' ||
            ee.type === 'slider' ||
            ee.type === 'switch'
          ) {
            ctrlsobject.isShowButReport = true
          }
        })
        break
      default:
        break
    }
  }
}

export function getCotrlCelValue(type, record, text, dragType) {
  try {
    if (text === null || text === undefined || text === '') {
      return text
    }
    let valuejson = {}
    switch (type) {
      case 'input':
        break
      case 'textarea':
        break
      case 'select':
        if (text.indexOf('[') >= 0) {
          valuejson = JSON.parse(text)
          return valuejson.join(';')
        }
        break
      case 'date':
        break
      case 'radio':
        if (text.indexOf('[') >= 0) {
          valuejson = JSON.parse(text)
          return valuejson.join(';')
        }
        break
      case 'checkbox':
        if (text.indexOf('[') >= 0) {
          valuejson = JSON.parse(text)
          return valuejson.join(';')
        }
        break
      case 'switch':
        return text === '1' ? '是' : '否'
      case 'slider':
        break
      case 'colorpicker':
        break
      case 'editor':
        break
      case 'rate':
        break
      case 'inputnumber':
        break
      case 'timepicker':
        break
      case 'user':
      case 'organ':
      case 'department':
      case 'role':
        if (text.indexOf('{') >= 0 && text.indexOf('}') >= 0) {
          valuejson = JSON.parse(text)
          return valuejson.text
        }
        return ''
      case 'cityselector':
        if (text.indexOf('[') >= 0) {
          valuejson = JSON.parse(text)
          let cityobj = cityjson
          let cityname = []
          valuejson.forEach(ee => {
            cityobj.forEach(ff => {
              if (ff.value === ee) {
                cityobj = ff.children
                cityname.push(ff.label)
              }
            })
          })
          return cityname.join(',')
        }

        return ''
      case 'datarelevance':
      case 'autonumber':
      case 'signname':
      case 'locationmap':
        break
      case 'upFilesComp':
        if (text.indexOf('[') >= 0) {
          if (dragType === 'file') {
            valuejson = JSON.parse(text)
            let imgstr = ''
            for (let i = 0; i < valuejson.length; i++) {
              imgstr =
                imgstr +
                ` <a href="${valuejson[i].url}" target="_blank">${valuejson[i].name}</a>`
            }
            return imgstr
          }
          valuejson = JSON.parse(text)
          let imgstr = ''
          for (let i = 0; i < valuejson.length; i++) {
            imgstr =
              imgstr +
              ` <img src="${valuejson[i].url}" width="40" height="40" onclick="showBigImage('${valuejson[i].url}')" title="${valuejson[i].name}"/>`
          }
          return imgstr
        }
        return ''
      default:
        break
    }
  } catch {
    console.log(text)
  }
  return text
}
